﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using IDAL.Cms;
using IDAL;
using Model.Cms;
using Model.Config;
using Model.AuthModel;
namespace SQLServerDAL.Cms
{
    /// <summary>
    /// 数据层，内容管理，资源管理
    /// </summary>
    public class ResourceDAL : DalBase, IResource
    {
        public ResourceDAL() 
        {
            Schema = "cms.";
            ViewTableName = "V_Resource";
            Fields = "ResourceID, ResourceName, ResourceLink, ContentSize, ContentType, ContentExtension, StaffID, IsOnline, OnlineTime, UpdateTime, AddTime, ResourceTypeID, ResourceTypeLabel, ResourceTypeName";
        }

        #region IResource 成员

        /// <summary>
        /// 新增资源下载
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool InsertResource(ResourceInfo info)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ResourceType",SqlDbType.Int)
                ,new SqlParameter("@ResourceName",SqlDbType.NVarChar,100)
                ,new SqlParameter("@ResourceLink",SqlDbType.NVarChar,100)
                ,new SqlParameter("@ContentSize",SqlDbType.VarChar,20)
                ,new SqlParameter("@ContentType",SqlDbType.VarChar,40)
                ,new SqlParameter("@ContentExtension",SqlDbType.VarChar,5)
                ,new SqlParameter("@StaffID",SqlDbType.Int)
                ,new SqlParameter("@IsOnline",SqlDbType.Char,1)
                ,new SqlParameter("@OnlineTime",SqlDbType.DateTime)
                ,new SqlParameter("RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = info.ResourceType.TermID;
            parm[i++].Value = info.ResourceName;
            parm[i++].Value = info.ResourceLink;
            parm[i++].Value = info.ContentSize;
            parm[i++].Value = info.ContentType;
            parm[i++].Value = info.ContentExtension;
            parm[i++].Value = info.Staff.StaffID;
            parm[i++].Value = info.IsOnline;
            parm[i++].Value = info.OnlineTime;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "AddResource", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 修改资源
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool UpdateResource(ResourceInfo info)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ResourceID",SqlDbType.Int)
                ,new SqlParameter("@ResourceType",SqlDbType.Int)
                ,new SqlParameter("@ResourceName",SqlDbType.NVarChar,100)
                ,new SqlParameter("@ResourceLink",SqlDbType.NVarChar,100)
                ,new SqlParameter("@ContentSize",SqlDbType.VarChar,20)
                ,new SqlParameter("@ContentType",SqlDbType.VarChar,40)
                ,new SqlParameter("@ContentExtension",SqlDbType.VarChar,5)
                ,new SqlParameter("@StaffID",SqlDbType.Int)
                ,new SqlParameter("@IsOnline",SqlDbType.Char,1)
                ,new SqlParameter("@OnlineTime",SqlDbType.DateTime)
                ,new SqlParameter("RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = info.ResourceID;
            parm[i++].Value = info.ResourceType.TermID;
            parm[i++].Value = info.ResourceName;
            parm[i++].Value = info.ResourceLink;
            parm[i++].Value = info.ContentSize;
            parm[i++].Value = info.ContentType;
            parm[i++].Value = info.ContentExtension;
            parm[i++].Value = info.Staff.StaffID;
            parm[i++].Value = info.IsOnline;
            parm[i++].Value = info.OnlineTime;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "UpdateResource", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 根据编号获取资源
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResourceInfo GetResourceByID(int id)
        {//ResourceID, ResourceName, ResourceLink, ContentSize, ContentType, ContentExtension, StaffID, IsOnline, OnlineTime, UpdateTime, AddTime, ResourceTypeID, ResourceTypeName
            Sql = string.Format("SELECT TOP 1 {0} FROM {1} WHERE ResourceID = {2}", Fields, Schema + ViewTableName, id);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, Sql);
            ResourceInfo info = new ResourceInfo();
            if (dr != null) 
            {
                if (dr.Read()) 
                {
                    int i = 0;
                    info.ResourceID = dr.GetInt32(i++);
                    info.ResourceName = dr.GetString(i++);
                    info.ResourceLink = dr.GetString(i++);
                    info.ContentSize = dr.GetString(i++);
                    info.ContentType = dr.GetString(i++);
                    info.ContentExtension = dr.GetString(i++);
                    info.Staff = new StaffInfo();
                    info.Staff.StaffID = dr.GetInt32(i++);
                    info.IsOnline = dr.GetString(i++);
                    info.OnlineTime = dr.GetDateTime(i++);
                    info.UpdateTime = dr.GetDateTime(i++);
                    info.AddTime = dr.GetDateTime(i++);
                    info.ResourceType = new TermInfo();
                    info.ResourceType.TermID = dr.GetInt32(i++);
                    info.ResourceType.TermLabel = dr.GetString(i++);
                    info.ResourceType.TermName = dr.GetString(i++);
                }
                dr.Close();
            }
            return info;
        }

        /// <summary>
        /// 资源列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<ResourceInfo> ListResource(ResourceCondition condition)
        {
            SqlBuilder = new StringBuilder("SELECT ");
            if (condition.Top > 0)
                SqlBuilder.AppendFormat(" TOP({0}) ", condition.Top);
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1 ", Fields, Schema + ViewTableName);
            if (condition.ResourceTypeID > 0)
                SqlBuilder.AppendFormat(" AND ResourceTypeID = {0}", condition.ResourceTypeID);
            if (!string.IsNullOrEmpty(condition.ResourceTypeLabel))
                SqlBuilder.AppendFormat(" AND ResourceTypeLabel = '{0}'", condition.ResourceTypeLabel);
            if (!string.IsNullOrEmpty(condition.OrderBy))
                SqlBuilder.AppendFormat(" ORDER BY {0} ", condition.OrderBy);
            List<ResourceInfo> list = new List<ResourceInfo>();
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());            
            if (dr != null)
            {
                while (dr.Read())
                {
                    ResourceInfo info = new ResourceInfo();
                    int i = 0;
                    info.ResourceID = dr.GetInt32(i++);
                    info.ResourceName = dr.GetString(i++);
                    info.ResourceLink = dr.GetString(i++);
                    info.ContentSize = dr.GetString(i++);
                    info.ContentType = dr.GetString(i++);
                    info.ContentExtension = dr.GetString(i++);
                    info.Staff = new StaffInfo();
                    info.Staff.StaffID = dr.GetInt32(i++);
                    info.IsOnline = dr.GetString(i++);
                    info.OnlineTime = dr.GetDateTime(i++);
                    info.UpdateTime = dr.GetDateTime(i++);
                    info.AddTime = dr.GetDateTime(i++);
                    info.ResourceType = new TermInfo();
                    info.ResourceType.TermID = dr.GetInt32(i++);
                    info.ResourceType.TermLabel = dr.GetString(i++);
                    info.ResourceType.TermName = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        /// <summary>
        /// 资源列表
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IList<ResourceInfo> ListResource(ResourceCondition condition, out int recordCount)
        {
            Wheres = new StringBuilder(" 1 = 1 ");
            if (condition.ResourceTypeID > 0)
                Wheres.AppendFormat(" AND ResourceTypeID = {0}", condition.ResourceTypeID);
            if (!string.IsNullOrEmpty(condition.ResourceTypeLabel))
                Wheres.AppendFormat(" AND ResourceTypeLabel = '{0}'", condition.ResourceTypeLabel);
            recordCount = base.PageListCounter(Schema + ViewTableName, Wheres.ToString());
            List<ResourceInfo> list = new List<ResourceInfo>();
            SqlDataReader dr = base.PageListRecord(Fields, Schema + ViewTableName, Wheres.ToString()
                , condition.OrderBy, condition.PageIndex, condition.PageSize);
            if (dr != null)
            {
                while (dr.Read())
                {
                    ResourceInfo info = new ResourceInfo();
                    int i = 0;
                    info.ResourceID = dr.GetInt32(i++);
                    info.ResourceName = dr.GetString(i++);
                    info.ResourceLink = dr.GetString(i++);
                    info.ContentSize = dr.GetString(i++);
                    info.ContentType = dr.GetString(i++);
                    info.ContentExtension = dr.GetString(i++);
                    info.Staff = new StaffInfo();
                    info.Staff.StaffID = dr.GetInt32(i++);
                    info.IsOnline = dr.GetString(i++);
                    info.OnlineTime = dr.GetDateTime(i++);
                    info.UpdateTime = dr.GetDateTime(i++);
                    info.AddTime = dr.GetDateTime(i++);
                    info.ResourceType = new TermInfo();
                    info.ResourceType.TermID = dr.GetInt32(i++);
                    info.ResourceType.TermLabel = dr.GetString(i++);
                    info.ResourceType.TermName = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }
        #endregion
    }
}
